package cn.chiship.framework.upms.biz.user.controller;


import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.sdk.core.annotation.NoParamsSign;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.annotation.Authorization;

import javax.annotation.Resource;

import cn.chiship.framework.upms.biz.user.service.UpmsUserOrganizationService;
import cn.chiship.framework.upms.biz.user.entity.UpmsUserOrganization;
import cn.chiship.framework.upms.biz.user.entity.UpmsUserOrganizationExample;
import cn.chiship.framework.upms.biz.user.pojo.dto.UpmsUserOrganizationDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 用户组织关联表控制层
 * 2023/2/21
 *
 * @author lijian
 */
@RestController
@Authorization
@RequestMapping("/userOrganization")
@Api(tags = "用户组织关联控制器")
public class UpmsUserOrganizationController extends BaseController<UpmsUserOrganization, UpmsUserOrganizationExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsUserOrganizationController.class);

    @Resource
    private UpmsUserOrganizationService upmsUserOrganizationService;

    @Override
    public BaseService getService() {
        return upmsUserOrganizationService;
    }

    @ApiOperation(value = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "所属单位", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字（姓名、手机、职位）", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "includeChildren", value = "是否包含下级部门(1:包含 2：不包含)", dataTypeClass = Byte.class, paramType = "query"),
            @ApiImplicitParam(name = "includeOrgs", value = "是否包含所加入组织(1:包含 2：不包含)", defaultValue = "2", dataTypeClass = Byte.class, paramType = "query"),

    })
    @Authorization
    @NoParamsSign
    @GetMapping(value = "/pageByOrgId")
    public ResponseEntity<BaseResult> pageByOrgId(
            @RequestParam(required = false, defaultValue = "1", value = "page") Long page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") Long limit,
            @RequestParam(required = false, value = "orgId") String orgId,
            @RequestParam(required = false, value = "keyword") String keyword,
            @RequestParam(required = false, value = "includeChildren") Byte includeChildren,
            @RequestParam(required = false, value = "includeOrgs", defaultValue = "2") Byte includeOrgs) {
        HashMap<String, Object> paramsMap = new HashMap();
        paramsMap.put("keyword", keyword);
        paramsMap.put("includeChildren", includeChildren);
        paramsMap.put("includeOrgs", includeOrgs);
        return super.responseEntity(upmsUserOrganizationService.pageUserByOrgId(page, limit, orgId, paramsMap));
    }

    @ApiOperation(value = "校验用户是否在组织下存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织", required = true, dataTypeClass = String.class, paramType = "query"),
    })
    @Authorization
    @GetMapping(value = "validateExist")
    public ResponseEntity<BaseResult> validateExist(
            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "orgId") String orgId) {
        return new ResponseEntity<>(upmsUserOrganizationService.validateExist(userId, orgId), HttpStatus.OK);
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_SAVE, describe = "保存用户组织关联表")
    @ApiOperation(value = "保存用户组织关联表")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid UpmsUserOrganizationDto upmsUserOrganizationDto) {
        UpmsUserOrganization upmsUserOrganization = new UpmsUserOrganization();
        BeanUtils.copyProperties(upmsUserOrganizationDto, upmsUserOrganization);
        return super.responseEntity(upmsUserOrganizationService.insertSelective(upmsUserOrganization));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE, describe = "更新用户组织关联表")
    @ApiOperation(value = "更新用户组织关联表")
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid UpmsUserOrganizationDto upmsUserOrganizationDto) {
        UpmsUserOrganization upmsUserOrganization = new UpmsUserOrganization();
        BeanUtils.copyProperties(upmsUserOrganizationDto, upmsUserOrganization);
        return super.responseEntity(super.update(id, upmsUserOrganization));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE, describe = "移除用户")
    @ApiOperation(value = "移除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键ID集合", required = true, dataTypeClass = List.class, paramType = "body")
    })
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody List<String> ids) {
        UpmsUserOrganizationExample upmsUserOrganizationExample = new UpmsUserOrganizationExample();
        upmsUserOrganizationExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(upmsUserOrganizationExample));
    }
}